<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "admin";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$colname_jadwal = $_POST['nik'];
$colname_jadwal2 = $_POST['hari'];
$colname_jadwal3 = $_POST['jam'];
$colname_jadwal4 = $_GET['a'];
$colname_jadwal5 = $_GET['b'];
$colname_jadwal6 = $_POST['tahun_ajaran'];
$colname_jadwal7 = $_POST['kd_matpel'];

mysql_select_db($database_learnConn, $learnConn);
$query_jadwal = sprintf("SELECT * FROM jadwal WHERE kd_kelas=%s AND kd_kat_kelas=%s AND hari=%s AND jam=%s AND tahun_ajaran=%s",
						GetSQLValueString($colname_jadwal4, "int"),
						GetSQLValueString($colname_jadwal5, "text"),
						GetSQLValueString($colname_jadwal2, "text"),
						GetSQLValueString($colname_jadwal3, "text"),
						GetSQLValueString($colname_jadwal6, "text"));
$jadwal = mysql_query($query_jadwal, $learnConn) or die(mysql_error());
$row_jadwal = mysql_fetch_assoc($jadwal);
$totalRows_jadwal = mysql_num_rows($jadwal);

mysql_select_db($database_learnConn, $learnConn);
$query_filterJadwal = sprintf("SELECT * FROM jadwal WHERE nik=%s AND hari=%s AND jam=%s",
						GetSQLValueString($colname_jadwal, "int"),
						GetSQLValueString($colname_jadwal2, "text"),
						GetSQLValueString($colname_jadwal3, "text"));
$filterJadwal = mysql_query($query_filterJadwal, $learnConn) or die(mysql_error());
$row_filterJadwal = mysql_fetch_assoc($filterJadwal);
$totalRows_filterJadwal = mysql_num_rows($filterJadwal);

mysql_select_db($database_learnConn, $learnConn);
$query_filterJadwal2 = sprintf("SELECT * FROM jadwal WHERE kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s",
						GetSQLValueString($colname_jadwal4, "int"),
						GetSQLValueString($colname_jadwal5, "text"),
						GetSQLValueString($colname_jadwal7, "text"));
$filterJadwal2 = mysql_query($query_filterJadwal2, $learnConn) or die(mysql_error());
$row_filterJadwal2 = mysql_fetch_assoc($filterJadwal2);
$totalRows_filterJadwal2 = mysql_num_rows($filterJadwal2);
   
	if($totalRows_jadwal == 0) {
	 if($totalRows_filterJadwal == 0) {
	  if($totalRows_filterJadwal2 < 5) {
		$insertSQL = sprintf("INSERT INTO jadwal (kd_jadwal, kd_kelas, kd_kat_kelas, tahun_ajaran, kd_matpel, nik, hari, jam) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['kd_jadwal'], "int"),
                       GetSQLValueString($_POST['kd_kelas'], "text"),
                       GetSQLValueString($_POST['kd_kat_kelas'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['kd_matpel'], "text"),
                       GetSQLValueString($_POST['nik'], "text"),
                       GetSQLValueString($_POST['hari'], "text"),
                       GetSQLValueString($_POST['jam'], "text"));
					   mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($insertSQL, $learnConn) or die(mysql_error());
  /** yoho
mysql_select_db($database_learnConn, $learnConn);
$query_siswa = sprintf("SELECT * FROM kelas WHERE kd_kelas=%s",
						GetSQLValueString($colname_jadwal4, "text"));
$filterSiswa = mysql_query($query_siswa, $learnConn) or die(mysql_error());
$row_filterSiswa = mysql_fetch_assoc($filterSiswa);
$totalRows_filterSiswa = mysql_num_rows($filterSiswa);

mysql_select_db($database_learnConn, $learnConn);
$query_siswa = sprintf("SELECT * FROM siswa_kelas WHERE kd_kelas=%s",
						GetSQLValueString($colname_jadwal4, "text"));
$filterSiswa = mysql_query($query_siswa, $learnConn) or die(mysql_error());
$row_filterSiswa = mysql_fetch_assoc($filterSiswa);
$totalRows_filterSiswa = mysql_num_rows($filterSiswa);

  
  $insertSQLNilai = sprintf("INSERT INTO nilai (nis, kd_kelas, kd_kat_kelas, tahun_ajaran, kd_matpel) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($nis, "text"),
                       GetSQLValueString($_POST['kd_kelas'], "text"),
                       GetSQLValueString($_POST['kd_kat_kelas'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['kd_matpel'], "text"));
  mysql_select_db($database_learnConn, $learnConn);
  $Result2 = mysql_query($insertSQLNilai, $learnConn) or die(mysql_error());
  */
  $insertGoTo = "jadwal.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
	  } // tutup if ketiga
	  else {
		  header(sprintf("Location: "."jadwalFail.php"));
	  }
	 }//tutup if kedua
	  else {
		  header(sprintf("Location: "."jadwalFail.php"));
	  }
	} //tutup if pertama
	else {
		header(sprintf("Location: "."jadwalFail.php"));
	}
   
}

$colname_matpel = "-1";
if (isset($_GET['a'])) {
  $colname_matpel = $_GET['a'];
}
mysql_select_db($database_learnConn, $learnConn);
$query_matpel = sprintf("SELECT * FROM matpel WHERE kd_matpel LIKE %s", GetSQLValueString("%" . $colname_matpel . "%", "text"));
$matpel = mysql_query($query_matpel, $learnConn) or die(mysql_error());
$row_matpel = mysql_fetch_assoc($matpel);
$totalRows_matpel = mysql_num_rows($matpel);

$kd_matpel = $row_matpel['kd_matpel'];
mysql_select_db($database_learnConn, $learnConn);
$query_guru = "SELECT m.nik, g.nama FROM pegawai g, guru_matpel m WHERE m.kd_matpel='$kd_matpel' AND jabatan='Guru' AND g.nik=m.nik";
$guru = mysql_query($query_guru, $learnConn) or die(mysql_error());
$row_guru = mysql_fetch_assoc($guru);
$totalRows_guru = mysql_num_rows($guru);

mysql_select_db($database_learnConn, $learnConn);
$query_jam = "SELECT * FROM jam";
$jam = mysql_query($query_jam, $learnConn) or die(mysql_error());
$row_jam = mysql_fetch_assoc($jam);
$totalRows_jam = mysql_num_rows($jam);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tambah Jadwal</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />

<script language="JavaScript" type="text/JavaScript">
 
 function showKab()
 {
 <?php
 // membaca semua kd_matpel
 //$query_matpel2 = "SELECT * FROM kd_matpel";
 $query_matpel2 = sprintf("SELECT * FROM matpel WHERE kd_matpel LIKE %s", GetSQLValueString("%" . $colname_matpel . "%", "text"));
 $hasil = mysql_query($query_matpel2);
 
 // membuat if untuk masing-masing pilihan kd_matpel beserta isi option untuk combobox kedua
// echo "coooy"; 
 while ($data = mysql_fetch_array($hasil))
 {
   //$idProp = $data['idProp'];
   $idProp = $data['kd_matpel'];

   // membuat IF untuk masing-masing kd_matpel
   echo "if (document.form1.kd_matpel.value == \"".$idProp."\")";
   echo "{";

   // membuat option kabupaten untuk masing-masing kd_matpel
   //$query2 = "SELECT * FROM kab WHERE idProp = $idProp";
   //$query2 = "SELECT * FROM guru_matpel WHERE kd_matpel = $idProp";
   //SELECT * FROM guru_matpel WHERE kd_matpel = 'AGM8'
   //$query2 = sprintf("SELECT * FROM guru_matpel WHERE kd_matpel = '$idProp'");
   $query2 = sprintf("SELECT distinct gm.nik, p.nama FROM guru_matpel gm, pegawai p WHERE gm.kd_matpel = '$idProp' AND gm.nik=p.nik");
   $hasil2 = mysql_query($query2);
   $content = "document.getElementById('kabupaten').innerHTML = \"";
   while ($data2 = mysql_fetch_array($hasil2))
   {
	   //$content .= "<option value='".$data2['idKab']."'>".$data2['namaKabupaten']."</option>";   
       $content .= "<option value='".$data2['nik']."'>".$data2['nama']."</option>";   
   }
   $content .= "\"";
   echo $content;
   echo "}\n";   
 }

 ?> 
 }
</script>
<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="800" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">Admin Menu :
      <ul>
        	<li><a href="index.php">Home</a></li>
            <li><a href="siswa.php">Siswa</a></li>
            <li><a href="pegawai.php">Pegawai</a></li>
            <li><a href="matpel.php">Mata Pelajaran</a></li>
            <li><a href="jadwal.php">Jadwal</a></li>
            <li><a href="pengumuman.php">Pengumuman</a></li>
            <li><a href="buku_tamu.php">Buku Tamu</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten">
		<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
          <table align="center">
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Kelas:</td>
                
                <td>
                <input type="hidden" name="kd_kelas" value="<?php echo $_GET['a']; ?>" />
                <input type="hidden" name="kd_kat_kelas" value="<?php echo $_GET['b']; ?>" />
                <?php echo $_GET['a']."-".$_GET['b']; ?></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tahun Ajaran:</td>
                <td>
                <?php $date = getdate();
				$tahun = $date['year'];
				$tahun2 = $date['year']+1;
				$gabung = $tahun."/".$tahun2;
				?>
                <select name="tahun_ajaran">
                  <option value="<?php echo $gabung; ?>"><?php echo $gabung; ?></option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Mata Pelajaran :</td>
                <td>
				<select name="kd_matpel" onchange="showKab()">
          <option>Pilih</option>          
          <?php
                 // query untuk menampilkan kd_matpel
                 //$query_matpel2 = "SELECT * FROM kd_matpel";
				 $query_matpel2 = sprintf("SELECT * FROM matpel WHERE kd_matpel LIKE %s", GetSQLValueString("%" . $colname_matpel . "%", "text"));
                 $hasil = mysql_query($query_matpel2);
                 while ($data = mysql_fetch_array($hasil))
                 {
                    //echo "<option value='".$data['idProp']."'>".$data['namaPropinsi']."</option>";
                    echo "<option value='".$data['kd_matpel']."'>".$data['nama_matpel']."</option>";
                 }
          ?>
          </select>
				<!--
				<select name="kd_matpel">
                  <?php // do {  ?>
                  <option value="<?php //echo $row_matpel['kd_matpel']?>" ><?php //echo $row_matpel['nama_matpel']?></option>
                  <?php// } while ($row_matpel = mysql_fetch_assoc($matpel));?>
                </select>
				-->
				</td>
              </tr>
              <tr> </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Guru Pengajar:</td>
                <td>
				<select name="nik" id="kabupaten"></select>
				<!--
				<select name="nik">
                  <?php //do {  ?>
                  <option value="<?php //echo $row_guru['nik']?>" ><?php //echo $row_guru['nama']?></option>
                  <?php //} while ($row_guru = mysql_fetch_assoc($guru)); ?>
                </select>
				-->
				</td>
              </tr>
              <tr> </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Hari:</td>
                <td><select name="hari">
                  <option value="Senin" <?php if (!(strcmp("Senin", ""))) {echo "SELECTED";} ?>>Senin</option>
                  <option value="Selasa" <?php if (!(strcmp("Selasa", ""))) {echo "SELECTED";} ?>>Selasa</option>
                  <option value="Rabu" <?php if (!(strcmp("Rabu", ""))) {echo "SELECTED";} ?>>Rabu</option>
                  <option value="Kamis" <?php if (!(strcmp("Kamis", ""))) {echo "SELECTED";} ?>>Kamis</option>
                  <option value="Jumat" <?php if (!(strcmp("Jumat", ""))) {echo "SELECTED";} ?>>Jumat</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Jam:</td>
                <td>
                    <select name="jam"><?php do { ?>
                      <option value="<?php echo $row_jam['jam']; ?>"><?php echo $row_jam['jam']; ?></option><?php } while ($row_jam = mysql_fetch_assoc($jam)); ?>
                    </select>
                    </td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">&nbsp;</td>
                <td><input type="submit" value="Tambah Jadwal" /></td>
              </tr>	
          </table>
          <input type="hidden" name="kd_jadwal" value="" />
            <input type="hidden" name="MM_insert" value="form1" />
          </form>
        <p>&nbsp;</p></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>
<?php
/*
mysql_free_result($matpel);

mysql_free_result($guru);

mysql_free_result($jam);

mysql_free_result($filterJadwal);
*/

?>